import FPJDefine from "../../Game/FPJ/Prefabs/FPJDefine";

const { ccclass, property } = cc._decorator;

@ccclass
export default class RoomButton extends cc.Component {
    @property(cc.Sprite)
    sp_choice: cc.Sprite = null;
    @property(cc.Sprite)
    sp_unchoice: cc.Sprite = null;
    private _index: Number = 0;
    private _type: Number = 0; // 大厅和小大厅
    private _roomid: Number = 0; // roomid
    _touchend: cc.Component.EventHandler[] = [];
    @property(cc.Label)
    idlenumlb: cc.Label = null;
    onLoad(){
        this.node.on(cc.Node.EventType.TOUCH_END, this.TouchEnd, this);
        cc.vv.eventCustom.on("showsp", this.showsp.bind(this), this);
        cc.vv.eventCustom.on("GameChange", () => {
            this.refreshNum();
        }, this.node); //监听服务器数据事件
        this.refreshNum();
    }

    onDisable() {
        //this.node.off(cc.Node.EventType.TOUCH_END, this.TouchEnd, this);
    };
    TouchEnd() {
        this._touchend && cc.Component.EventHandler.emitEvents(this._touchend, this._index);
        cc.vv.eventCustom.emit("showsp", this._index)
        this.refreshNum();
    }
    initData(index, spchoice, spunchoice, _touchend, type, roomid) {
        this.sp_choice.spriteFrame = spchoice;
        this.sp_unchoice.spriteFrame = spunchoice
        this._touchend = _touchend;
        this._index = index;
        this._type = type;
        this._roomid = roomid;
        this.refreshNum();
    }
    showsp(args) {
        // cc.error("this._index  is  ",this._index, "  args[0]  is  ",args[0])
        if (this._index == args[0]) {
            this.sp_choice.node.active = true;
            this.sp_unchoice.node.active = false;
        } else {
            this.sp_choice.node.active = false;
            this.sp_unchoice.node.active = true;
        }
    }

    refreshNum() {
        let t = FPJDefine.room_list[0]
        for (var op = 0; op < FPJDefine.room_list.length; op++) {
            if (FPJDefine.room_list[op].room_id == this._roomid) {
                t = JSON.parse(JSON.stringify(FPJDefine.room_list[op]));
            }
        }
        if (!t || !t.table_list) {
            return;
        }
        let num = 0;
        t.table_list.forEach(element => {
            if (!element.sitdown_player && !element.hold_table) {
                num++;
            }
        });
        this.idlenumlb.string = String(num);
    }

    onclickidle() {
        let t = FPJDefine.room_list[0]
        for (var op = 0; op < FPJDefine.room_list.length; op++) {
            if (FPJDefine.room_list[op].room_id == this._roomid) {
                t = FPJDefine.room_list[op];
                break;
            }
        }
        if (!t || !t.table_list) {
            return;
        }
        let tableid = 0;
        for (var s = 0; s < t.table_list.length; s++) {
            if (!t.table_list[s].sitdown_player && !t.table_list[s].hold_table) {  // 没有玩家 并且没有留机器
                tableid = t.table_list[s].table_id;
                break;
            }
        }
        if (tableid == 0) {
            cc.vv.GLUtils.showTips("没有空闲机器");
            return;
        }
        let sendEnterTable = () => {
            let _gameid = 109;
            cc.vv.GLUtils.sendRequest("ATTEnterTable", {
                table_id: tableid
            }, (error, data) => {
                if (!error) {
                    if (data.errorcode) {
                        cc.vv.GLUtils.showTips(data.errordes);
                    } else {
                        FPJDefine.setTableDetail(data.table_data);
                        FPJDefine.setcurrentTableId(tableid)
                        FPJDefine.setCurrentState(data.table_data.status)
                        if (this._type == 1) {
                            let _path = cc.vv.GLDefine.getEnglishNameById(_gameid);
                            cc.vv.UIMgr.open(cc.vv.GLConfig.waiting_path);
                            cc.vv.sceneMgr.openScene(_path, data, () => {
                                cc.vv.eventCustom.emit("openGame");
                                cc.vv.UIMgr.close(cc.vv.GLConfig.waiting_path, cc.vv.UIMgr.closeType.hide);
                            });
                        } else {
                            cc.vv.eventCustom.emit("resetTable");
                        }
                    }
                }
                // else {
                //     cc.vv.GLUtils.showTips(data.errordes);
                // }
            });
        }

        if (this._type == 2) {  //走多台  要先发离座子消息
            cc.vv.GLUtils.sendRequest("ATTLeaveTable", {
                table_id: FPJDefine.getcurrentTableId(),   // 桌子ID
            }, (error, data) => {
                if (!data.errorcode) {
                    sendEnterTable()
                } else {
                    cc.vv.GLUtils.showTips(data.errordes);
                }
            });
        } else {
            sendEnterTable();
        }
    }
}
